﻿Imports UtilitiesLibrary
Imports System.Data.SqlClient

Public Class WebForm1
    Inherits System.Web.UI.Page
    Dim objDB As DBConnect
    Dim sql As String
    Dim gameID As Integer
    Dim gameName As String
    Dim dr As SqlDataReader
    Dim DisplayFlag_OneChecked As Boolean = False 'checks for at least 1 system checked off in grid
    Dim systemID As New ArrayList
    Dim goalID As New ArrayList

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        objDB = Session("db")

        If Not IsPostBack Then
            sql = "SELECT * FROM System"
            gvSystemList.DataSource = objDB.getDataSet(sql)
            gvSystemList.DataBind()

            btnAddGoal.Visible = False
            btnCreateGame.Visible = False
        End If
    End Sub

    Protected Sub btnAddSystem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAddSystem.Click
        objDB = Session("db")

        If ValidateGameName() <> "" Then
            lblError1.Text = ValidateGameName()
            Exit Sub
        Else
            txtGameName.ReadOnly = True
            gvSystemList.Enabled = False
            btnAddSystem.Enabled = False
            btnAddGoal.Visible = True

            gameName = txtGameName.Text
            lblError1.Text = "CHECK - Game Name is: " & gameName

            Dim CBox As CheckBox
            'Dim SystemName As String = ""


            For i As Integer = 0 To (gvSystemList.Rows.Count - 1)

                CBox = CType(gvSystemList.Rows(i).FindControl("chkSelect"), CheckBox)

                If CBox.Checked Then
                    DisplayFlag_OneChecked = True
                    'SystemName = gvSystemList.Rows(i).Cells(2).Text

                    'sql = "SELECT SystemID FROM System WHERE SystemName = '" & SystemName.Trim & "'"

                    'dr = objDB.getDataReader(sql)
                    'dr.Read()

                    Dim sysID As Integer = CInt(gvSystemList.Rows(i).Cells(1).Text)

                    systemID.Add(sysID)

                    'dr.Close()
                End If
            Next

            Dim systemIDArray As String = "("

            Dim pntr As Integer

            For pntr = 0 To (systemID.Count - 2)
                systemIDArray += systemID(pntr).ToString & ", "
            Next

            systemIDArray += systemID(pntr).ToString & ")"

            lblError2.Text = "CHECK - System IDs added to the game are: " & systemIDArray

            sql = "SELECT DISTINCT Goal.GoalID, Goal.goalName FROM Goal JOIN Goal_SystemAttribute ON Goal.GoalID = Goal_SystemAttribute.GoalID AND Goal_SystemAttribute.SystemID IN " & systemIDArray

            gvGoals.DataSource = objDB.getDataSet(sql)
            gvGoals.DataBind()

        End If

    End Sub

    Private Function ValidateGameName() As String
        If txtGameName.Text = "" Then
            Return "Name field cannot be empty"
        Else
            Return ""
        End If

    End Function

    Protected Sub btnAddGoal_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAddGoal.Click
        'objDB = Session("db")

        gvGoals.Enabled = False
        btnAddGoal.Enabled = False
        btnCreateGame.Visible = True

        Dim CBox As CheckBox

        For i As Integer = 0 To (gvGoals.Rows.Count - 1)

            CBox = CType(gvGoals.Rows(i).FindControl("chkSelect"), CheckBox)

            If CBox.Checked Then
                DisplayFlag_OneChecked = True
                Dim gID As Integer = CInt(gvGoals.Rows(i).Cells(1).Text)

                goalID.Add(gID)
            End If
        Next

        Dim goalIDArray As String = "("

        Dim pntr As Integer

        For pntr = 0 To (goalID.Count - 2)
            goalIDArray += goalID(pntr).ToString & ", "
        Next

        goalIDArray += goalID(pntr).ToString & ")"

        lblError3.Text = "CHECK - Goal IDs added to the game are: " & goalIDArray

    End Sub

    Protected Sub btnCreateGame_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCreateGame.Click
        If MsgBox("Are you sure you want to create the game?", vbYesNo) = vbYes Then
            btnCreateGame.Enabled = False
            gameName = txtGameName.Text

            Dim sql1 = "INSERT INTO Game VALUES ('" & gameName & "', 'ModifierSlotID')"
            Dim sql2 = "FOR ALL ELEMENTS IN systemID (ArrayList): INSERT INTO Game_System VALUES ('gameID', 'systemID(i)')"
            Dim sql3 = "FOR ALL ELEMENTS IN goalID (ArrayList): INSERT INTO Game_Goal VALUES ('gameID', 'goalID(i)')"
            Dim sql4 = ""

            lblCreated01.Text = "Game Name: " & gameName
            lblCreated02.Text = sql1
            lblCreated03.Text = sql2
            lblCreated04.Text = sql3

        End If
    End Sub
End Class